/**
 * Created by yuxiang on 2016-09-18.
 */

import com.dangdang.ddframe.job.api.JobConfiguration;
import com.dangdang.ddframe.job.api.JobExecutionMultipleShardingContext;
import com.dangdang.ddframe.job.api.JobScheduler;
import com.dangdang.ddframe.job.api.listener.AbstractDistributeOnceElasticJobListener;
import com.dangdang.ddframe.job.plugin.job.type.integrated.ScriptElasticJob;
import com.dangdang.ddframe.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.reg.zookeeper.ZookeeperRegistryCenter;


public final class JobDemo {

    private final ZookeeperConfiguration zkConfig = new ZookeeperConfiguration("localhost:2181", "elasticjob-example");

    private final CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zkConfig);

    private final JobConfiguration simpleJobConfig = new JobConfiguration("simpleElasticDemoJob", MyElasticJob.class, 10, "0 0/1 * * * ?");



    // CHECKSTYLE:OFF
    public static void main(final String[] args) {
        // CHECKSTYLE:ON
        new JobDemo().init();
    }

    public void init() {
        zkConfig.setNestedPort(2181);
        zkConfig.setNestedDataDir(String.format("target/test_zk_data/%s/", System.nanoTime()));
        regCenter.init();
        new JobScheduler(regCenter, simpleJobConfig, new SimpleDistributeOnceElasticJobListener()).init();

    }

    class SimpleDistributeOnceElasticJobListener extends AbstractDistributeOnceElasticJobListener {

        SimpleDistributeOnceElasticJobListener() {
            super(1000L, 1000L);
        }

        @Override
        public void doBeforeJobExecutedAtLastStarted(final JobExecutionMultipleShardingContext shardingContext) {
            System.out.println("------ before simple job start ------");
        }

        @Override
        public void doAfterJobExecutedAtLastCompleted(final JobExecutionMultipleShardingContext shardingContext) {
            System.out.println("------ after simple job start ------");
        }
    }
}
